﻿using BW.WMS.WareHouseManage;
using BW.WMS.WareHouseStatistics.Statistics;
using NUglify.JavaScript.Syntax;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace BW.WMS.WareHouseStatistics
{
    public class DashboardAppService : ApplicationService, IDashboardAppService
    {
        private readonly IRepository<StockManagementModel> _StockRepository;
        private readonly IRepository<Warehouse> _HouseRepository;

        public DashboardAppService(IRepository<StockManagementModel> StockRepository, IRepository<Warehouse> HouseRepository)
        {
            _StockRepository = StockRepository;
            _HouseRepository = HouseRepository;
        }

        /// <summary>
        /// 获取库存数据
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<ChartDataDto>> GetSalesDataForChartAsync()
        {
            var stockquery = await _StockRepository.GetListAsync();
            var housequery = await _HouseRepository.GetListAsync();


            var query = from stock in stockquery
                        join house in housequery on stock.Warehouseid equals house.Id
                        group new { stock, house } by house.Name into grouped
                        select new ChartDataDto
                        {
                            Name = grouped.Key,  // 仓库名称
                            Stocknumber = grouped.Sum(x => x.stock.Stocknumber ?? 0),  // 入库数量
                            Stockmoney = grouped.Sum(x => x.stock.Stockmoney ?? 0)  // 金额
                        };

            var result = query.ToList();

            return result;
        }
    }
}
